package com.blog.dxxr.blogfront.Controller;

import com.blog.dxxr.blogfront.Common.Domain.ReturnObject;
import com.blog.dxxr.blogfront.Common.contants.Contants;
import com.blog.dxxr.blogfront.Common.utils.DateUtils;
import com.blog.dxxr.blogfront.Common.utils.UUIDUtils;
import com.blog.dxxr.blogfront.Domain.Article;
import com.blog.dxxr.blogfront.Domain.ArticleComment;
import com.blog.dxxr.blogfront.Service.ArticleBrowseService;
import com.blog.dxxr.blogfront.Service.ArticleCommentService;
import com.blog.dxxr.blogfront.Service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BlogDetailController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleCommentService articleCommentService;
    @Autowired
    private ArticleBrowseService articleBrowseService;

    //异步查询文章前一篇和后一篇文章
    @RequestMapping("/blogDetails/queryProAndNextArticle.do")
    public @ResponseBody Object queryProAndNextArticle(String id){
        String createDate = articleService.queryArticleCreateDateById(id);
        Article articlePro = articleService.selectProArticleById(createDate);
        Article articleNext = articleService.selectNextArticleById(createDate);
        Map<String,Object> map = new HashMap<>();
        map.put("articlePro",articlePro);
        map.put("articleNext",articleNext);
        return map;
    }

    //异步上传评论
    @RequestMapping("/blogDetails/saveArticleComment.do")
    public @ResponseBody Object saveArticleComment(ArticleComment articleComment){
        ReturnObject object = new ReturnObject();
        articleComment.setId(UUIDUtils.getUUID());
        articleComment.setCommentDate(DateUtils.formatDataTime(new Date()));
        try {
            int ret = articleCommentService.saveCommentWithArticle(articleComment);
            if (ret > 0){
                object.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                object.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                object.setMessage("系统繁忙，暂无法发布评论");
            }
        }catch (Exception e){
            e.printStackTrace();
            object.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            object.setMessage("系统繁忙，暂无法发布评论");
        }
        return object;
    }

    //异步查询文章评论
    @RequestMapping("/blogDetails/queryArticleComment.do")
    public @ResponseBody Object queryArticleComment(String articleid){
        List<ArticleComment> articleCommentList = articleCommentService.queryCommentByArticleId(articleid);
        return articleCommentList;
    }

    //异步查询文章评论数和访问量
    @RequestMapping("/blogDetails/queryArticleAllTotal.do")
    public @ResponseBody Object queryArticleAllTotal(String articleId){
        int commentTotal = articleCommentService.selectCommentTotalByArticleId(articleId);
        articleBrowseService.updateTotalByArticleId(articleId);
        int browseTotal = articleBrowseService.queryBrowseTotal(articleId);
        Map<String,Object> map = new HashMap<>();
        map.put("commentTotal",commentTotal);
        map.put("browseTotal",browseTotal);
        return map;
    }


    //跳转回首页
    @RequestMapping("/blogDetails/backIndex.do")
    public String backIndex(){
        return "redirect:/";
    }
}
